kotlin random number

47

kotlin random number -

val rnds = (0..10).random() // generated random from 0 to 10 included

random class in kotlin -

import kotlin.random.Random
 
fun rand(start: Int, end: Int): Int {
    require(start <= end) { "Illegal Argument" }
    val rand = Random(System.nanoTime())
    return (start..end).random(rand)
}
 
fun main() {
    val start = 5
    val end = 9
 
    for (i in 1..5) println(rand(start, end))
}

Comments

Submit
0 Comments